package com.funtl.spring.cloud.alibaba.business.config;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName LoginFilter
 * @Description TODO
 * @Author Mr.Starry
 * @Date 2019/12/24 21:50
 * @Version 1.0
 */

public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse)servletResponse;
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        String uri = req.getRequestURI();

        if (uri.equals("/login")||uri.equals("/login.html")){
            filterChain.doFilter(req,resp);
            return;
        }
        String userLoginInfo = (String)req.getSession().getAttribute("userLoginInfo");
        if (userLoginInfo == null){
            resp.sendRedirect("/login.html");
        }else {
            filterChain.doFilter(req,resp);
            return;
        }
    }
}
